package com.briup.smartlabs.web.controller;
import java.util.List;
import com.briup.smartlabs.common.utils.Response;
import com.briup.smartlabs.mapper.SysUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.briup.smartlabs.bean.SysUser;
import io.swagger.annotations.Api;
@Api(tags = "管理员管理服务")
@RestController
public class UserController {
    @Autowired
    private SysUserMapper userMapper;
    @ApiOperation(value = "返回用户所有信息")
    @GetMapping("/user/all")
//    public List<SysUser> findByName(String name){
    public Response<List<SysUser>> findAll(){
        List<SysUser> users = userMapper.selectAll();
        return Response.ok(users);
//        return null;
    }
    @ApiOperation(value = "分页显示所以用户信息")
    @GetMapping("/user/by/page")
    public Response<PageInfo<SysUser>> findByPage(
            @RequestParam(defaultValue = "5")int pageSize,
            @RequestParam(defaultValue = "1")int pageNum){
        PageHelper.startPage(pageNum, pageSize,true);
        List<SysUser> users = userMapper.selectAll();
        PageInfo<SysUser> pages = new PageInfo<>(users);
        return Response.ok(pages);
    }
    @ApiOperation(value = "删除用户信息")
    @DeleteMapping("/user/by/id")
    public Response<String> delecteById(SysUser id){
        if (id==null){
            return Response.error("请输入要删除的用户Id!");
        }
        int count = userMapper.updateByPrimaryKey(id);
        if (count>0){
            return Response.ok("删除成功！");
        }
        return Response.ok("删除成功");
    }
    @ApiOperation(value = "更新用户信息")
    @PostMapping("/user")
    public Response<String> saveOrUpdate(SysUser user){
        if (user.getUserId()== null){
            userMapper.insert(user);
        }
        else {
            userMapper.updateByPrimaryKey(user);
        }
        return Response.ok("操作成功！");
    }
}